package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.AuthThreadLocal;
import com.itheima.common.JwtToken;
import com.itheima.mapper.AddressBookMapper;
import com.itheima.pojo.dto.AddressBookDto.AddressBookAddDto;
import com.itheima.pojo.dto.AddressBookDto.AddressBookUpdateDto;
import com.itheima.pojo.vo.addressBookVo.AddressBookByIdVo;

import com.itheima.pojo.vo.addressBookVo.GetAddressBookVo;
import com.itheima.pojo.vo.addressBookVo.GetDefaultAddressBookVo;
import com.itheima.service.AddressBookService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author tcotk
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Resource
    private AddressBookMapper mapper;


    @Override
    public boolean add(AddressBookAddDto addressBook) {

        String userId = JwtToken.parse(AuthThreadLocal.getAmount());
        String id = IdUtil.getSnowflakeNextIdStr();
        addressBook.setId(id);


        addressBook.setUserId(userId);
        addressBook.setCreateUser(userId);

        addressBook.setCreateTime(LocalDateTime.now());

        int add = mapper.add(addressBook);
        return add > 0;
    }

    @Override
    public boolean update(AddressBookUpdateDto addressBook) {
        String amount = AuthThreadLocal.getAmount();
        String userId = JwtToken.parse(amount);
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setUpdateUser(userId);
        int update = mapper.update(addressBook);
        return update > 0;
    }

    @Override
    public boolean delete(Integer[] ids) {
        int result = 0;
        for (Integer id : ids) {
            result = mapper.delete(id);
            result += result;
        }
        return result > 0;
    }

    @Override
    public List<GetAddressBookVo> selectAll() {

        return mapper.selectAll();
    }

    @Override
    public AddressBookByIdVo selectById(String id) {
        return   mapper.selectById(id);

    }

    @Override
    public GetDefaultAddressBookVo selectByIsDefault() {


                return mapper.selectByIsDefauly();

    }

    @Override
    public GetDefaultAddressBookVo updateDef(String id) {
        int size = mapper.selectAll().size();
        String userId = JwtToken.parse(AuthThreadLocal.getAmount());
        if (size == 1) {
            AddressBookUpdateDto addressBook2=new AddressBookUpdateDto();
            addressBook2.setId(id);
            addressBook2.setUpdateTime(LocalDateTime.now());


            addressBook2.setUpdateUser(userId);
            addressBook2.setIsDefault(1);
            int update = mapper.update(addressBook2);
            if (update > 0) {
                return mapper.selectByIsDefauly();
            }
        } else if (size > 1) {
            AddressBookUpdateDto addressBook1=new AddressBookUpdateDto();

            String id1 = mapper.selectByIsDefauly().getId();
            addressBook1.setId(id1);
            addressBook1.setIsDefault(0);
            addressBook1.setUpdateUser(userId);
            int update1 = mapper.update(addressBook1);
            AddressBookUpdateDto addressBook3=new AddressBookUpdateDto();
           addressBook3.setId(id);
            addressBook3.setIsDefault(1);
            int update = mapper.update(addressBook3);
            if (update > 0 && update1 > 0) {
                return mapper.selectByIsDefauly();
            }
        }
        return null;
    }
}
